package com.terrace.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.terrace.core.common.model.ResultData;
import com.terrace.db.system.mapper.entity.DataSource;
import com.terrace.db.system.service.DataSourceService;
import com.terrace.core.mybatis.entity.Pagination;
import com.terrace.web.annotation.Logs;
import com.terrace.web.constant.GlobalsConstant;
import com.terrace.web.model.DataGrid;

/**
 * 数据源
 * @author jiangyg
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/datasource")
public class DataSourceController {
	
	private static final String MESSAGE_TYPE = "数据源";
	
	@Autowired
	private DataSourceService dataSourceService;
	
	/**
	 * 默认管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView dataSourceList(HttpServletRequest request){
		return new ModelAndView("system/dataSource/dataSourceList");
	}
	
	/**
	 * 数据表格
	 * @param request
	 * @param pagination
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "datagrid")
	@RequiresPermissions("system:datasource:view")
	public DataGrid<DataSource> datagrid(DataSource dataSource, Pagination<DataSource> pagination, HttpServletRequest request) {
		DataGrid<DataSource> dataGrid = new DataGrid<DataSource>();
		dataGrid.setPagination(dataSourceService.selectDataSourceForPagination(pagination, dataSource));
		return dataGrid;
	}
	
	/**
	 * 新增或者修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(@RequestParam(value = "dbName", required = false) String name,
			HttpServletRequest request) {
		if (StringUtils.isNotBlank(name)) {
			DataSource dataSource = dataSourceService.selectDataSourceByName(name);
			request.setAttribute("dataSource", dataSource);
		}
		return new ModelAndView("system/dataSource/dataSource");
	}
	
	/**
	 * 保存数据源
	 * @param dataSource
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequestMapping(params = "saveOrUpdate")
	@RequiresPermissions("system:datasource:save")
	@Logs(depict = "数据源保存", type = GlobalsConstant.LogTypes.SYS_DATASOURCE)
	public ResultData saveOrUpdate(DataSource dataSource, HttpServletRequest request) {
		ResultData rd = new ResultData();
		DataSource temp = dataSourceService.selectDataSourceByName(dataSource.getDbName());
		if (temp == null) {
			dataSourceService.insertDataSourceSelective(dataSource);
			rd.successObjs("10001", MESSAGE_TYPE);
		} else {
			dataSourceService.updateDataSourceByNameSelective(dataSource);
			rd.successObjs("10002", MESSAGE_TYPE);
		}
		return rd;
	}
	
	/**
	 * 删除数据源
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "delete")
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequiresPermissions("system:datasource:delete")
	@Logs(depict = "删除数据源", type = GlobalsConstant.LogTypes.SYS_DATASOURCE)
	public ResultData delete(@RequestParam(value = "name") String name, HttpServletRequest request) {
		ResultData rd = new ResultData();
		DataSource dataSource = dataSourceService.selectDataSourceByName(name);
		if (dataSource == null) {
			return rd.failObjs("13103", MESSAGE_TYPE);
		}
		int result = dataSourceService.deleteDataSourceByName(name);
		if (result != 1) {
			rd.failObjs("13102", MESSAGE_TYPE);
		} else {
			rd.successObjs("10003", MESSAGE_TYPE);
		}
		return rd;
	}

}
